<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 1000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    chartData: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  watch:{
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(dataset){
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: (params)=> {
            //console.log(params)
            let res='<div class=tooltipStyle><div class=list><div class=listTitle>'+params[0].data.time+'</div><div>离线时长：'+params[0].data.minute+'min</div><div>'+params[0].data.area_name+' - '+params[0].data.type_name+' - '+params[0].data.no+'</div><div>卡头ID：'+params[0].data.clevice_id+'</div></div></div>';
            return res;
          },
          padding: [5, 10]
        },
        grid: {
          // top: 10,
          // left: '2%',
          // right: '2%',
          // bottom: '3%',
          left: 20,
          right: 55,
          bottom: 20,
          top: 50,
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          name:"卡头ID",
          //data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          name:"分钟",
          axisTick: {
            show: false
          }
        }],
        dataset:dataset,
        // dataset:{
        //   dimensions: ['product', "time"],
        //   source:[
        //     {"product":"12345",time:"300"},
        //     {"product":"12346",time:"120"},
        //     {"product":"12347",time:"80"},
        //     {"product":"12348",time:"110"},
        //     {"product":"12349",time:"50"},
        //     {"product":"12310",time:"30"},
        //     {"product":"12311",time:"260"}
        //   ]
        // },
        series: [{
          //name: 'pageA',
          type: 'bar',
          //数据堆叠
          //stack: 'vistors',
          barWidth: '20%',
          //data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration ,
          itemStyle:{
            normal:{
              color:function(params){
                //console.log(params)
                var index_num = params.data.minute;
                if(index_num>120){
                  return "#bd2b2b"
                }else if(index_num<=120&&index_num>60){
                  return "#ff4242"
                }else if(index_num<=60&&index_num>30){
                  return "#ff8f2b"
                }else{
                  return "#fdc927"
                }
              }
            }
          },
        }, 
        // {
        //   name: 'pageB',
        //   type: 'bar',
        //   stack: 'vistors',
        //   barWidth: '60%',
        //   data: [80, 52, 200, 334, 390, 330, 220],
        //   animationDuration
        // }, {
        //   name: 'pageC',
        //   type: 'bar',
        //   stack: 'vistors',
        //   barWidth: '60%',
        //   data: [30, 52, 200, 334, 390, 330, 220],
        //   animationDuration
        // }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData);
    }
  }
}
</script>
